<template>
  <div>
    <van-notify id="van-notify" />
    <van-card  desc="欢迎使用教室占用系统" :title="userInfo.nickName" :thumb="userInfo.avatarUrl"/>
      <van-col span="24" v-for="(lendClass, index) in AlllendClass" :key="index">
        <div v-show="(index === 0)?true:false" ><van-divider contentPosition="center" customStyle="color: #1989fa;border-color: #1989fa;font-size: 18px;" value="我的申请">我的申请</van-divider></div>
        <van-collapse :value="activeNames" @change="onChange" >
          <van-collapse-item class="title" :title="`${lendClass.date}    ${lendClass.classroom}    ${lendClass.startTime}-${lendClass.endTime}`" :name="index">
            <p>班级/部门: {{lendClass.department}}</p><br>
            <p>用途: {{lendClass.use}}</p><br>
            <p>联系方式: {{lendClass.phonenumber}}</p>
            <van-button type="info" color="green" size="mini" @click="update(lendClass._id)">修改</van-button><span>&nbsp;&nbsp;</span>
            <van-button type="info" size="mini" color="red" @click="dalete(lendClass._id)">删除</van-button>
          </van-collapse-item>
        </van-collapse>
      </van-col>
    <i-cell-group>
      <i-cell title="关于" is-link url="/pages/profile/about/index"></i-cell>
    </i-cell-group>
  </div>
</template>

<script>
  import DateFormat from "../../utils/dateFormat";
  import Notify from 'static/vant/notify/notify';
  export default {
    name: "index",
    data() {
      return{
        userInfo: {},
        AlllendClass: [],
        activeNames: ['1'],
        isShow: false,
        dateTime : DateFormat(new Date().toDateString())
      }
    },
    onShow() {
      this.restart()
    },
    onLoad() {
      this.userInfo = this.$store.state.userInfo
      this.restart()
    },
    methods: {
      restart() {
        const db = wx.cloud.database({env: 'xxzx1201-gxxma'});
        // 获取当天教室占用情况
        db.collection('lendClass').where({ _openid: this.userInfo.openid }).orderBy('classroom', 'asc').get({
          success: (res) =>  this.AlllendClass = res.data
        })
      },
      onChange(event) {
        this.activeNames = event.detail
      },
      dalete(id) {
        const db = wx.cloud.database({env: 'xxzx1201-gxxma'});
        db.collection('lendClass').doc(id).remove({
          success: (res) => {
            Notify({ type: 'success', message: '删除成功', duration:1000});
            this.restart()
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          }
        })
      },
      update(e) {
        this.$store.commit('saveId', e)
        wx.navigateTo({
          url: '/pages/update/index',
        })
      },
    }
  }
</script>

<style scoped>

</style>
